package com.sjy.ylxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.dto.QueryPatientDTO;
import com.sjy.ylxt.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.sjy.ylxt.mapper.YlxtPatientMapper;
import com.sjy.ylxt.entity.domain.YlxtPatient;
import com.sjy.ylxt.service.YlxtPatientService;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: 史俊颖
* @CreateTime: 2024-12-12
* @Description: ${description}
*/
@Service
@Slf4j
public class YlxtPatientServiceImpl extends ServiceImpl<YlxtPatientMapper, YlxtPatient>  implements YlxtPatientService{

    @Resource
    private YlxtPatientMapper ylxtPatientMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ylxtPatientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(YlxtPatient record) {
        return ylxtPatientMapper.insert(record);
    }

    @Override
    public int insertSelective(YlxtPatient record) {
        return ylxtPatientMapper.insertSelective(record);
    }

    @Override
    public YlxtPatient selectByPrimaryKey(Integer id) {
        return ylxtPatientMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(YlxtPatient record) {
        return ylxtPatientMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(YlxtPatient record) {
        return ylxtPatientMapper.updateByPrimaryKey(record);
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public ResultBody<?> queryPatient(QueryPatientDTO dto) {
        try{
            log.info("==> 开始分页查询患者列表");
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
            LambdaQueryWrapper<YlxtPatient> qw = new LambdaQueryWrapper<>();
            qw.eq(StringUtils.isNotEmpty(dto.getEmail()),YlxtPatient::getEmail,dto.getEmail());
            qw.eq(StringUtils.isNotEmpty(dto.getPhoneNumber()),YlxtPatient::getPhoneNumber,dto.getPhoneNumber());
            qw.eq(StringUtils.isNotEmpty(dto.getUserName()),YlxtPatient::getUserName,dto.getUserName());
            qw.orderByDesc(YlxtPatient::getCreateTime);
            List<YlxtPatient> ylxtPatients = ylxtPatientMapper.selectList(qw);
            PageInfo<YlxtPatient> pageInfo = new PageInfo<>(ylxtPatients);
            log.info("==> 分页查询患者列表成功");
            return ResultBody.success(pageInfo);
        }catch (Exception e){
            log.error("==> 查询患者列表失败：{}",e.getMessage());
            return ResultBody.error("查询患者列表失败");
        }
    }

}
